class Order < ActiveRecord::Base
  has_many :order_items
  
  PAYMENT_TYPES = [
    # Label       Value in db
    ["Cash on delivery",    "cash"],
    ["Electronic Cash",     "ec"],
    ["Credit Card",         "cc"]
    ]
    
  validates_presence_of :name, :firstName, :street, :streetNr, :city, :zip, :email, :telephone, :pay_method
  # next line says in my 2D array PAYMENT_TYPES, if the first of each pair is called disp, and the second
  # is called value, make sure that when the form is submitted, there is something given for value, 
  # and that it's one of those in PAYMENT_TYPES.
  validates_inclusion_of :pay_method, :in => PAYMENT_TYPES.map {|disp, value| value}
  validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
  
  #no spaces, no numbers, no extra signs
  validates_format_of :name, :with => /\A([A-Za-z]+)\Z/i
  validates_format_of :firstName, :with => /\A([A-Za-z]+)\Z/i
  
  #only numbers
  validates_format_of :zip, :with => /\A([0-9]{4,})\Z/i
  validates_format_of :streetNr, :with => /\A([0-9]+([-A-Za-z]?)+)\Z/i
  
  #validates_format_of :telephone, :with => /\A([+?\d\/?\-?\(?\)?\s?]+){3,}\Z/i
  validates_format_of :telephone, :with => /\A(([\d]{3,})?(\([\d]{3,6}\)\s[\d]{3,})?(\+\d{2,}\s[\d]{3,})?([\d]{3,6}\s[\d]{3,})?([\d]{3,6}(\/?\-?)[\d]{3,})?)\Z/i
  #validation for address -> address better in single parts divided....

  
  def add_order_items_from_cart(cart)
	#array has to be created HERE
	order_items = Array.new
	cart.cart_items.each do |item|
	  oi = OrderItem.from_cart_item(item)
      order_items << oi
    end
  end
    
end


# == Schema Information
#
# Table name: orders
#
#  id         :integer         not null, primary key
#  name       :string(255)
#  email      :string(255)
#  pay_method :string(10)
#  created_at :datetime
#  updated_at :datetime
#  firstName  :string(255)
#  telephone  :string(255)
#  street     :string(255)
#  city       :string(255)
#  zip        :string(255)
#  streetNr   :string(255)
#

